﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using MyChannel.DownloadComponent;
using System.Windows.Data;
using System.Windows.Media.Imaging;
using System.IO;
using System.IO.IsolatedStorage;

namespace MyChannel
{
    public partial class DownloadingPage : PhoneApplicationPage
    {
        public DownloadingPage()
        {
            InitializeComponent();
            if (ModelLocator.HomeStatic.Downloads.Count == 0)
            {
                bool empty = true;
                if (App.ViewModel != null)
                    foreach (LocalVideo video in App.ViewModel.VideoList)
                    {
                        if (video.State != "Completed")
                        {
                            empty = false;
                            Download(video);
                        }
                    }
                if (empty)
                {
             
                }
                else
                {
                    dlList.Visibility = Visibility.Visible;
             
                }
            }
            foreach (Download dl in ModelLocator.HomeStatic.Downloads)
            {
                if (dl.Video.State == "Downloading")
                    return;
            }
            if (ModelLocator.HomeStatic.Downloads.Count > 0)
                ModelLocator.HomeStatic.Downloads[0].StartDownload();
        }


        public static void Download(LocalVideo item)
        {
            if (item != null)
            {
                if (ModelLocator.HomeStatic.Downloads.Count > 0)
                {
                    item.State = "Pending";
                    ModelLocator.HomeStatic.Downloads.Add(new Download() { Video = item });
                }
                else
                {
                    item.State = "Downloading";
                    Download dl = new Download() { Video = item };
                    ModelLocator.HomeStatic.Downloads.Add(dl);
                    dl.StartDownload();
                }
            }
        }

        private void Image_MouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            Download dl = (sender as Image).Tag as Download;
            ModelLocator.HomeStatic.Downloads.Remove(dl);
            if (dl.Video.State == "Downloading")
            {
                dl.StopDownload();
                if (ModelLocator.HomeStatic.Downloads.Count != 0)
                {
                    foreach (Download d in ModelLocator.HomeStatic.Downloads)
                    {
                        if (d.Video.State == "Pending")
                        {
                            d.StartDownload();
                            break;
                        }
                    }
                }
            }
            App.ViewModel.DeleteVideo(dl.Video);
        }

        private void Image_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            Image img = sender as Image;
            img.Width = 35;
            img.Height = 35;
        }
    }
    
}